package com.ruoyi;

import com.coupang.openapi.sdk.Hmac;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * 简单的退货地列表查询测试 - 不带查询参数
 */
public class SimpleReturnCenterTest {

    private static final String HOST = "api-gateway.coupang.com";
    private static final int PORT = 443;
    private static final String SCHEMA = "https";
    
    private static final String ACCESS_KEY = "fe8a5ff1-28be-4a36-8694-577cc7960166";
    private static final String SECRET_KEY = "f05224e25ebb0c237564da5cce5be106926c7930";

    public void test() {
        String method = "GET";
        String vendorId = "A158000DM0";
        // 先测试不带查询参数的请求
        String path = "/v2/providers/openapi/apis/api/v1/vendors/" + vendorId + "/returnShippingCenters";

        CloseableHttpClient client = null;
        try {
            client = HttpClients.createDefault();
            
            // 完全按照 HmacProductCreation 的方式
            String authorization = Hmac.generate(method, path, SECRET_KEY, ACCESS_KEY);
            System.out.println(authorization);
            System.out.println("Method: " + method);
            System.out.println("Path: " + path);

            URIBuilder uriBuilder = new URIBuilder()
                    .setScheme(SCHEMA)
                    .setHost(HOST)
                    .setPort(PORT)
                    .setPath(path);
            
            HttpGet requestGet = new HttpGet(uriBuilder.build().toString());

            requestGet.addHeader("Authorization", authorization);
            requestGet.addHeader("X-Requested-By", vendorId);
            requestGet.addHeader("X-MARKET", "KR");
            requestGet.addHeader("Content-Type", "application/json");
            
            System.out.println("\n请求 URL: " + uriBuilder.build().toString());
            
            CloseableHttpResponse response = null;
            try {
                System.out.println("\n发送请求（不带查询参数）...");
                response = client.execute(requestGet);
                
                System.out.println("\nstatus code: " + response.getStatusLine().getStatusCode());
                System.out.println("status message: " + response.getStatusLine().getReasonPhrase());
                HttpEntity entity = response.getEntity();
                System.out.println("result: " + EntityUtils.toString(entity));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (response != null) {
                    try {
                        response.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("========================================");
        System.out.println("简单测试：退货地列表查询（不带查询参数）");
        System.out.println("========================================\n");
        
        SimpleReturnCenterTest test = new SimpleReturnCenterTest();
        test.test();
    }
}

